// pages/collectionClass/collectionClass.js

// 使用云数据库
const db = wx.cloud.database();

// 引入获取当前时间类
const util = require('util.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid:wx.getStorageSync('openid'),
        // 导航栏初始值
        state:'farming_tab',
        buttonColor_1: '#46d5fa',
        buttonColor_2: '#78e2fb',
        top:'linear-gradient(to right, rgb(53,210,250),rgb(67,213,250),rgb(92,218,251),rgb(92,219,254))',
        border_bottom:'5rpx solid #46d5fa',
        // 农技教学课程初始数据
        collectionsFarming_list:[],

        // 农技教学课程整合数据
        Farming_list:[],

        // 普法栏目课程初始数据
        collectionsLaws_list:[],

        // 普法栏目课程整合数据
        Laws_list:[],
    },

    // 加载农技教学模块
    showFarming:function(){
        this.setData({
            state: 'farming_tab',
            buttonColor_1: '#46d5fa',
            buttonColor_2: '#78e2fb',
            top:'linear-gradient(to right, rgb(53,210,250),rgb(67,213,250),rgb(92,218,251),rgb(92,219,254))',
            border_bottom:'5rpx solid #46d5fa',
        })
    },

    // 加载普法栏目模块
    showLaws:function(){
        this.setData({
            state: 'laws_tab',
            buttonColor_1: '#ffeaa4',
            buttonColor_2: '#fdda67',
            top:'linear-gradient(to top, #fdda67 ,#fddf7a,#ffe590,#ffeaa4 )',
            border_bottom:'5rpx solid #fdda67',
        })
    },

    // 加载课程数据
    loadData:function(){
        let that = this
        wx.showLoading({
          title: '加载中',
        })
        // 获取课程数据
        db.collection('user-information').where({
            _openid:that.data.openid,
        }).get().then(async res =>{
            // console.log(res.data[0])
            that.setData({
                collectionsFarming_list:res.data[0].collectionsFarming_list,
                collectionsLaws_list:res.data[0].collectionsLaws_list
            })
            let tempfarmingList = []
            let templawsList = []
            let  promiseAll = new Promise((resloveAll,reject)=>{
                let promise1
                let promise2
                // 整合农技教学数据
                for(let i = 0;i<that.data.collectionsFarming_list.length;i++){
                    let id = that.data.collectionsFarming_list[i]._id
                    let timestamp = that.data.collectionsFarming_list[i].timestamp
                    let date = util.getDateDiff(new Date(timestamp));
                    let obj = {}
                    promise1 = new Promise((reslove1,reject)=>{
                        db.collection('farmingVideo').where({
                            _id:id
                        }).get().then(res=>{
                            console.log(res);
                            obj = {
                                id:res.data[0]._id,
                                cover:res.data[0].upload_cover,
                                title:res.data[0].title,
                                info:res.data[0].info.split('&br').join(' '),
                                addtime:date
                            }
                            tempfarmingList.push(obj)
                            if(i+1 == that.data.collectionsFarming_list.length){
                                reslove1()
                            }
                        })
                    })
                }
                promise1.then(res=>{ 
                    // 整合普法课程数据
                    for(let j = 0;j<that.data.collectionsLaws_list.length;j++){
                        let id = that.data.collectionsLaws_list[j]._id
                        let timestamp = that.data.collectionsLaws_list[j].timestamp
                        let date = util.getDateDiff(new Date(timestamp));
                        let obj = {}
                        promise2 = new Promise((reslove2,reject)=>{
                            db.collection('lawsVideo').where({
                                _id:id
                            }).get().then(res=>{
                                console.log(res);
                                obj = {
                                    id:res.data[0]._id,
                                    cover:res.data[0].upload_cover,
                                    title:res.data[0].title,
                                    info:res.data[0].info.split('&br').join(''),
                                    addtime:date
                                }
                                templawsList.push(obj)
                                if(j+1 == that.data.collectionsLaws_list.length){
                                    reslove2()
                                }
                            })
                        })
                        promise2.then(res=>{ 
                            // templawsList.push(obj)
                            setTimeout(function(){
                                resloveAll()
                            },1000)
                        })  
                    }
                })
            })
            promiseAll.then(res=>{ 
                that.setData({
                    Farming_list:tempfarmingList,
                    Laws_list:templawsList
                })
                wx.hideLoading({
                    success: (res) => {
                        wx.showToast({
                            title: '加载成功',
                            icon:'success'
                        })
                    },
                })
            })
        })
    },

    // 跳转农技教学详情页面
    jumpFarmingVideoDetail:function(e){
        console.log(e)
        let data_id = e.currentTarget.dataset.id;
        console.log(data_id);
        // 将内容id作为参数传到详情页
        wx.navigateTo({
            url: '/pages/FarmingvideoDetail/FarmingvideoDetail?id='+data_id,
        })
    },

    // 跳转普法栏目详情页面
    jumpLawsVideoDetail:function(e){
        console.log(e)
        let data_id = e.currentTarget.dataset.id;
        console.log(data_id);
        // 将内容id作为参数传到详情页
        wx.navigateTo({
            url: '/pages/LawsvideoDetail/LawsvideoDetail?id='+data_id,
        })
    },

    //返回上一个页面
    jumpgoback:function(){
        wx.navigateBack({
        delta: 1,
        })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        that.loadData()
    },
    

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})